<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Message_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function send()
    {
        $this->load->helper('MY_string');
        $this->load->helper('string');
        $code = $this->input->post('mcode');
        $data = array(
            'CategoryName' => $this->security->sanitize_filename($this->input->post('mtype')),
            'Detail' => substr($this->security->sanitize_filename($this->input->post('mcont')), 0, 300),
            'MyName' => $this->security->sanitize_filename($this->input->post('mname')),
            'Mobile' => $this->security->sanitize_filename($this->input->post('mtel')),
            'Email' => $this->security->sanitize_filename($this->input->post('memail')),
            'ip' => $this->security->sanitize_filename($this->input->ip_address())
        );
        $data = $this->security->xss_clean($data);

        foreach ($data as $key => $val) {
            $data[$key] = clean(quotes_to_entities(reduce_multiples($val, ",")));
        }


        foreach ($data as $key => $val) {
            if ($val == "") {
                unset($data);
            }
        }


        if (!isset($code) || $code == "") {
            return -1;
        }
        session_start();
        if ($code != $_SESSION['strcode']) {
            return -1;
        }
        if (isset($data)) {
            $this->db->insert("message", $data);
            $aid = $this->db->insert_id();
            $this->db->close();
            if ($aid > 0) {
                unset($_SESSION['strcode']);
                return 1;
            } else {
                return -2;
            }
        } else {
            return -2; //没有完整填写
        }

        //$this->db->affected_rows();

    }

}
